/*
 * $Id: RFSCreator.java,v 1.8 2011/01/14 17:04:44 troybirk Exp $
 * 
 * Copyright (C) 2010 General Electric Company. All Rights Reserved.
 * 
 * This software is the confidential and proprietary information of the General
 * Electric Company (GE). You shall not disclose this software and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with GE.
 * 
 * GE MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
 * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
 * NON-INFRINGEMENT. GE SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 * AS A RESULT OF USING, MODIFYING, OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES.
 */
package com.ge.healthcare.autosc.common.createrfs;

import com.ge.med.service.eds.caseAPI.CaseFactory;
import com.ge.med.service.eds.caseAPI.ICase;
import com.ge.med.service.eds.caseAPI.vo.AssistCaseCreateRequest;
import com.ge.med.service.eds.caseAPI.vo.AssistCaseCreateResponse;
import com.ge.med.service.eds.commons.DataException;
import com.ge.med.service.eds.commons.ResourceException;
import com.ge.med.service.eds.rfs.IRFS;
import com.ge.med.service.eds.rfs.ObjectFactory;
import com.ge.med.service.eds.rfs.vo.RFSCreateCAPS;
import com.ge.med.service.eds.rfs.vo.RFSList;
import com.ge.med.service.eds.rfs.vo.RFSResponseCAPS;
import com.ge.med.service.eds.rfs.vo.RFSSearchCriteria;

/**
 * Class that will handle creation of CAPS RFSs and Assist cases. Also includes
 * methods to search for existing RFSs/casses.
 * 
 */
public class RFSCreator {

    private static final String OPEN = "1";
    private static final String YES = "Y";
    private static final String NO = "N";
    private static final String EPOCH_DATE = "19700101";

    /**
     * Constructor.
     */
    public RFSCreator() {
        super();
    }

    /**
     * Determine if an RFS currently exists for the given system ID and rule ID.
     * Return the first open RFS number that for a system ID that matches the
     * specified ruleID.
     * 
     * @param systemId the system ID we are searching for. Null system ID will
     *            result in null being returned.
     * @param ruleId the rule ID we are searching for. Cannot be null.
     * @param toDate the end date to use when searching for existing RFS.
     * @param countryCode country code of the system ID being checked
     * @return RFS number if RFS is found matching criteria, otherwise null.
     * @throws DataException
     * @throws ResourceException
     */
    public String findRFSForSystem(String systemId, String ruleId, String toDate, String countryCode)
            throws ResourceException, DataException {
        RFSSearchCriteria sc = new RFSSearchCriteria();
        sc.setSystemId(systemId);
        sc.setCountryCode(countryCode);
        sc.setFromDate(EPOCH_DATE);
        sc.setToDate(toDate);
        sc.setIncludeClosed(NO);
        sc.setIncludeAssigned(YES);
        sc.setIncludeOpen(YES);

        RFSList[] rfsList = getIRFS().searchBySystemIdDateRange(sc);
        // Loop though the results looking for a match on rule id.
        for (RFSList rfs : rfsList) {
            if (rfs.getSymptoms() != null && rfs.getSymptoms().equalsIgnoreCase(ruleId)) {
                return rfs.getRFSNumber();
            }
            String ruleAndSymptom = rfs.getRuleId() + ":" + rfs.getSymptoms();

            if (ruleAndSymptom.equalsIgnoreCase(ruleId)) {
                return rfs.getRFSNumber();
            }
        }

        return null;

    }

    /**
     * Create a CAPS RFS.
     * 
     * @param vo the request message to be handled.
     * @return response message. The response message will contain a status that
     *         will indicate if the request was successful or not.
     * @throws DataException
     * @throws ResourceException
     */
    public RFSResponseCAPS createCAPSRFS(RFSCreateCAPS vo) throws ResourceException, DataException {

        IRFS irfs = getIRFS();
        return (RFSResponseCAPS) irfs.create(vo);
    }

    /**
     * Create an Assist Case.
     * 
     * @param assistobj Assist Case Creation Request object.
     * 
     * @return response message from ICase
     * @throws ResourceException thrown by ICase
     * @throws DataException thrown by ICase
     */

    public AssistCaseCreateResponse createAssistCase(AssistCaseCreateRequest assistobj) throws ResourceException,
            DataException {

        return (AssistCaseCreateResponse) getICase().createCase(assistobj);
    }

    /**
     * Getter for the IRFS interface.
     * 
     * @return instance of the IRFS interface.
     */
    public IRFS getIRFS() {
        return ObjectFactory.getInstance().getRFSObject();
    }

    /**
     * Getter for the ICase interface.
     * 
     * @return instance of the ICase interface.
     */
    public ICase getICase() {
        return CaseFactory.getInstance().getCaseObject();
    }

}
